const prefix = 'APP_',
  TOKEN = `${prefix}TOKEN`,
  LANG = `${prefix}LANG`,
  USERINFO = `${prefix}USERINFO`,
  CURRENTPAGE = `${prefix}CURRENTPAGE`,
  SIGN = `${prefix}SIGN`;
const auth = {
  //设置语言
  setLang: (lang = 'cn') => {
    localStorage.setItem(LANG, lang);
  },

  //设置语言
  getLang: () => {
    return localStorage.getItem(LANG) || 'cn';
  },

  //设置token
  setToken: (token: string) => {
    localStorage.setItem(TOKEN, token);
  },

  //获取token
  getToken: () => {
    return localStorage.getItem(TOKEN) || '';
  },

  //设置用户信息
  setUserInfo: (userInfo: object | string) => {
    if (typeof userInfo === 'object') {
      userInfo = JSON.stringify(userInfo);
    }
    localStorage.setItem(USERINFO, userInfo);
  },

  //获取用户信息
  getUserInfo: () => {
    let u = localStorage.getItem(USERINFO);
    if (u) {
      return JSON.parse(u);
    } else {
      return {}
    }
  },

  // 设置当前路由，对应home.vue菜单组件默认选中
  setCurrentPage: (path: string) => {
    localStorage.setItem(CURRENTPAGE, path);
  },

  // 获取当前路由，对应home.vue菜单组件默认选中
  getCurrentPage: () => {
    return localStorage.getItem(CURRENTPAGE) || ''
  },

  // 20220602 存储解密sign
  setDecryptSign(s: string) {
    localStorage.setItem(SIGN, s);
  },

  getDecryptSign() {
    // 获取解密sign
    return localStorage.getItem(SIGN) || ''
  },

  //删除token
  removeToken: () => {
    localStorage.removeItem(TOKEN);
    localStorage.removeItem(USERINFO);
    localStorage.removeItem(LANG);
    localStorage.removeItem(SIGN);
  }
};

export default auth;
